package com.lulin.mall.product.controller;

import java.util.Arrays;
import java.util.List;
import java.util.Map;

//import org.apache.shiro.authz.annotation.RequiresPermissions;
import com.lulin.mall.product.vo.OrderItemSpuInfoVO;
import com.lulin.mall.product.vo.SpuInfoVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

import com.lulin.mall.product.entity.SpuInfoEntity;
import com.lulin.mall.product.service.SpuInfoService;
import com.lulin.common.utils.PageUtils;
import com.lulin.common.utils.R;


/**
 * spu信息
 *
 * @author lulin
 * @email 2032327049@qq.com
 * @date 2022-09-21 14:25:13
 */
@RestController
@RequestMapping("product/spuinfo")
public class SpuInfoController {
    @Autowired
    private SpuInfoService spuInfoService;

    /**
     * 商品的上架功能
     * 地址：/product/spuinfo/42/up
     * 传递过来1个spuId
     * 我们根据spuId,查询出需要存在Es中的数据，
     * 然后，把数据报错到Es中，并修改该spu的状态为 上架
     *
     * @param spuId 商品spuId
     * @return
     */
    @PostMapping("/{spuId}/up")
    public R spuUp(@PathVariable("spuId") Long spuId) {
        //商品的上架功能
        spuInfoService.spuUp(spuId);
        return R.ok();
    }

    /**
     * 根据spuIds，查询出相关的spu信息
     *
     * @param spuIds spuid集合
     */
    @GetMapping("/getOrderItemSpuInfoBySpuId/{spuIds}")
    public List<OrderItemSpuInfoVO> getOrderItemSpuInfoBySpuId(@PathVariable("spuIds") Long[] spuIds) {
        return spuInfoService.getOrderItemSpuInfoBySpuId(spuIds);
    }


    /**
     * 查询spu列表信息
     */
    @RequestMapping("/list")
    public R list(@RequestParam Map<String, Object> params) {
        //默认是分页查询
        //PageUtils page = spuInfoService.queryPage(params);

        //根据多个条件，进行分页查询
        PageUtils page = spuInfoService.queryPageByConditon(params);

        return R.ok().put("page", page);
    }


    /**
     * 信息
     */
    @RequestMapping("/info/{id}")
    //@RequiresPermissions("product:spuinfo:info")
    public R info(@PathVariable("id") Long id) {
        SpuInfoEntity spuInfo = spuInfoService.getById(id);

        return R.ok().put("spuInfo", spuInfo);
    }

    /**
     * 保存商品的发布信息
     */
    @RequestMapping("/save")
    public R save(@RequestBody SpuInfoVO spuInfoVO) {
        spuInfoService.saveSpuInfo(spuInfoVO);
        return R.ok();
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
    //@RequiresPermissions("product:spuinfo:update")
    public R update(@RequestBody SpuInfoEntity spuInfo) {
        spuInfoService.updateById(spuInfo);

        return R.ok();
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
    //@RequiresPermissions("product:spuinfo:delete")
    public R delete(@RequestBody Long[] ids) {
        spuInfoService.removeByIds(Arrays.asList(ids));

        return R.ok();
    }

}
